package com.example.memento.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 词典查询结果DTO
 * 用于API响应的数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class DictionaryResult {
    
    @NotBlank(message = "单词不能为空")
    @Size(max = 100, message = "单词长度不能超过100个字符")
    private String word;
    
    @Size(max = 200, message = "发音长度不能超过200个字符")
    private String pronunciation;
    
    @Size(max = 500, message = "翻译长度不能超过500个字符")
    private String translation;
    
    @Size(max = 1000, message = "定义长度不能超过1000个字符")
    private String definition;
    
    @Size(max = 2000, message = "例句长度不能超过2000个字符")
    private String example;
    
    @Size(max = 100, message = "词性长度不能超过100个字符")
    private String partOfSpeech;
    
    @NotBlank(message = "词典源不能为空")
    @Size(max = 50, message = "词典源长度不能超过50个字符")
    private String source;
    
    @Size(max = 50, message = "语言长度不能超过50个字符")
    private String language;
    
    @Size(max = 50, message = "目标语言长度不能超过50个字符")
    private String targetLanguage;
    
    private List<String> tags;
    
    private Integer frequency;
    
    private Double confidence;
    
    private Double relevanceScore;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    /**
     * 从WordEntry创建DictionaryResult
     */
    public static DictionaryResult fromWordEntry(WordEntry wordEntry) {
        return DictionaryResult.builder()
                .word(wordEntry.getWord())
                .pronunciation(wordEntry.getPronunciation())
                .translation(wordEntry.getTranslation())
                .definition(wordEntry.getDefinition())
                .example(wordEntry.getExample())
                .partOfSpeech(wordEntry.getPartOfSpeech())
                .source(wordEntry.getSource())
                .language(wordEntry.getLanguage())
                .targetLanguage(wordEntry.getTargetLanguage())
                .tags(wordEntry.getTags())
                .frequency(wordEntry.getFrequency())
                .confidence(wordEntry.getConfidence())
                .createdAt(wordEntry.getCreatedAt())
                .updatedAt(wordEntry.getUpdatedAt())
                .build();
    }
    
    /**
     * 从WordEntry创建DictionaryResult并计算相关性分数
     */
    public static DictionaryResult fromWordEntry(WordEntry wordEntry, String query) {
        DictionaryResult result = fromWordEntry(wordEntry);
        result.setRelevanceScore(wordEntry.calculateRelevanceScore(query));
        return result;
    }
    
    /**
     * 转换为WordEntry
     */
    public WordEntry toWordEntry() {
        return WordEntry.builder()
                .word(this.word)
                .pronunciation(this.pronunciation)
                .translation(this.translation)
                .definition(this.definition)
                .example(this.example)
                .partOfSpeech(this.partOfSpeech)
                .source(this.source)
                .language(this.language)
                .targetLanguage(this.targetLanguage)
                .tags(this.tags)
                .frequency(this.frequency)
                .confidence(this.confidence)
                .createdAt(this.createdAt)
                .updatedAt(this.updatedAt)
                .build();
    }
    
    /**
     * 检查是否包含指定标签
     */
    public boolean hasTag(String tag) {
        return tags != null && tags.contains(tag);
    }
    
    /**
     * 检查是否来自指定源
     */
    public boolean isFromSource(String source) {
        return this.source != null && this.source.equals(source);
    }
    
    /**
     * 检查是否匹配语言
     */
    public boolean matchesLanguage(String language) {
        return this.language != null && this.language.equals(language);
    }
    
    /**
     * 获取主要翻译（取第一个翻译）
     */
    public String getPrimaryTranslation() {
        if (translation == null || translation.isEmpty()) {
            return null;
        }
        
        // 如果包含多个翻译（用/分隔），取第一个
        String[] translations = translation.split("/");
        return translations[0].trim();
    }
    
    /**
     * 获取所有翻译
     */
    public List<String> getAllTranslations() {
        if (translation == null || translation.isEmpty()) {
            return List.of();
        }
        
        return List.of(translation.split("/"))
                .stream()
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .toList();
    }
}
